package com.wl156.activities;

import java.io.IOException;

import org.json.JSONObject;
import org.ksoap2.serialization.SoapObject;
import org.xmlpull.v1.XmlPullParserException;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.widget.Toast;

import com.umeng.analytics.MobclickAgent;
import com.umeng.analytics.UmengOnlineConfigureListener;
import com.wl156.BaseActivity;
import com.wl156.MyApp;
import com.wl156.R;
import com.wl156.model.Certificate;
import com.wl156.model.WebServiceClient;
import com.wl156.services.DownloadIntentService;

public class Wl156Activity extends BaseActivity {

	private static final String TAG = Wl156Activity.class.getSimpleName();

	private Handler mHandler = new Handler();
	private WebServiceClient mWebServiceClient;
	private Certificate mCertificate;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		MobclickAgent.onError(this);
		MobclickAgent.updateOnlineConfig(this);
		setContentView(R.layout.main);

		MobclickAgent.setOnlineConfigureListener(new UmengOnlineConfigureListener(){
		      @Override
		      public void onDataReceived(JSONObject data) {
		    	  Log.d(TAG, "enable: JSONObject: " + data.toString());
		      }
		});
		String enable = MobclickAgent.getConfigParams(this, "canuse");
		Log.d(TAG, "enable: " + enable);
		if ("no".equals(enable)) {
			finish();
			return;
		}

		mWebServiceClient = new WebServiceClient(Wl156Activity.this);
		mCertificate = ((MyApp) getApplication()).getCertificateFromXml();

		new AsyncTask<Void, Void, Object>() {

			protected void onPostExecute(Object result) {
				try {
					if (result instanceof Exception) {
						Exception e = (Exception) result;

						Intent i = new Intent(Wl156Activity.this, MainTab.class);
						startActivity(i);
						finish();
					} else {
						SoapObject so = (SoapObject) result;
						String version = so.getProperty(0).toString();
						String appVersion = Wl156Activity.this
								.getString(R.string.version);
						final String url = so.getPropertyAsString(1);

						Log.d(TAG, "version: " + version + ", appVersion: "
								+ appVersion + ", url: " + url);
						if (Float.parseFloat(version) > Float
								.parseFloat(appVersion)) {

							// 下载新版
							AlertDialog.Builder builder = new Builder(
									Wl156Activity.this);
							builder.setTitle("有新版本");
							builder.setMessage("当前最新版本为：" + version + ", 是否升级？");
							builder.setPositiveButton("是",
									new OnClickListener() {

										@Override
										public void onClick(
												DialogInterface dialog,
												int which) {

											Toast.makeText(Wl156Activity.this,
													"有新的版本，正在下载中",
													Toast.LENGTH_LONG).show();
											Intent i = new Intent(
													Wl156Activity.this,
													DownloadIntentService.class);
											i.putExtra("url", url);
											Wl156Activity.this.startService(i);

											i = new Intent(Wl156Activity.this,
													MainTab.class);
											startActivity(i);
											finish();
										}
									});
							builder.setNegativeButton("否",
									new OnClickListener() {

										@Override
										public void onClick(
												DialogInterface dialog,
												int which) {
											Intent i = new Intent(
													Wl156Activity.this,
													MainTab.class);
											startActivity(i);
											finish();
										}
									});
							builder.show();

						} else {
							Intent i = new Intent(Wl156Activity.this,
									MainTab.class);
							startActivity(i);
							finish();
						}
					}
				} catch (Exception e) {
					Intent i = new Intent(Wl156Activity.this, MainTab.class);
					startActivity(i);
					finish();
				}

			};

			@Override
			protected Object doInBackground(Void... params) {
				try {
					return mWebServiceClient.common_AutoUpdate(mCertificate);
				} catch (IOException e) {
					e.printStackTrace();
					return e;
				} catch (XmlPullParserException e) {
					e.printStackTrace();
					return e;
				}
			}
		}.execute();

		// mHandler.postDelayed(new Runnable() {
		// @Override
		// public void run() {
		// // Class<? extends BaseActivity> startActivity = Login.class;
		// // if (((MyApp) getApplication()).isLogined()) {
		// // startActivity = HomeActivity.class;
		// // }
		//
		// }
		// }, 10);
	}
}